﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;
namespace YNCNumberSys
{
    class ZJGetTextStyleTableRecord
    {
         /// <summary>
        /// 事务,为空表示操作当前图纸空间.
        /// </summary>
        private Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 文字样式名称.
        /// </summary>
        private String _str_TextStyleName;

        public String str_TextStyleName
        {
            get { return _str_TextStyleName; }
            //set { _str_TextStyleName = value; }
        }
        /// <summary>
        /// 返回值,文字样式的对象标识符号.
        /// </summary>
        private ObjectId _id_TextStyleTableRecord;

        public ObjectId id_TextStyleTableRecord
        {
            get { return _id_TextStyleTableRecord; }
            //set { _id_TextStyleTableRecord = value; }
        }
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase和图纸文档均为当前图纸空间,并且图纸文档未锁定;
        /// (2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;
        public ZJGetTextStyleTableRecord(ACADDatabase wdb_DataBase, Transaction trs_Transaction, string str_name)
        {
            _wdb_DataBase = wdb_DataBase;
            _trs_Transaction = trs_Transaction;
            _str_TextStyleName = str_name;
            _b_TransactionIsUsed = true;
            _id_TextStyleTableRecord = ObjectId.Null;
        }

        public ZJGetTextStyleTableRecord(string str_name)
        {
            _wdb_DataBase = HostApplicationServices.WorkingDatabase; ;
            _trs_Transaction = null;
            _str_TextStyleName = str_name;
            _b_TransactionIsUsed = false;
            _id_TextStyleTableRecord = ObjectId.Null;
        }

        public bool Has()
        {
            bool has_TextStyleTableRecord = false;
            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    TextStyleTable tst_TextStyleTable = (TextStyleTable)_trs_Transaction.GetObject(_wdb_DataBase.TextStyleTableId, OpenMode.ForRead);
                    if (tst_TextStyleTable.Has(_str_TextStyleName))
                    {
                        has_TextStyleTableRecord = true;
                    }
                }
            }
            else
            {
                TextStyleTable tst_TextStyleTable = (TextStyleTable)_trs_Transaction.GetObject(_wdb_DataBase.TextStyleTableId, OpenMode.ForRead);
                if (tst_TextStyleTable.Has(_str_TextStyleName))
                {
                    has_TextStyleTableRecord = true;
                }
            }
            return has_TextStyleTableRecord;
        }

        public ObjectId Do()
        {
            _id_TextStyleTableRecord = ObjectId.Null;
            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    TextStyleTable tst_TextStyleTable = (TextStyleTable)_trs_Transaction.GetObject(_wdb_DataBase.TextStyleTableId, OpenMode.ForRead);
                    if (tst_TextStyleTable.Has(_str_TextStyleName))
                    {
                        _id_TextStyleTableRecord = tst_TextStyleTable[_str_TextStyleName];
                    }
                }
            }
            else
            {
                TextStyleTable tst_TextStyleTable = (TextStyleTable)_trs_Transaction.GetObject(_wdb_DataBase.TextStyleTableId, OpenMode.ForRead);
                if (tst_TextStyleTable.Has(_str_TextStyleName))
                {
                    _id_TextStyleTableRecord = tst_TextStyleTable[_str_TextStyleName];
                }
            }

            return _id_TextStyleTableRecord;
        }
    }
}
